﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.IO;
using System.Text;

namespace Util.IO
{
    // V 1.0.1 - 2021-12-09 09:09:26
    // 增加 GetFilesAdv 方法
    //
    // V 1.0.0 - 2020-08-18 16:57:22
    // 首次创建

    /// <summary>
    ///
    /// </summary>    
    public class DirectoryUtils
    {
        /// <summary>
        /// 获取文件夹内所有文件
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <param name="isSearchSubDirs">开启搜索子文件夹, 默认 true</param>
        /// <returns>返回文件路径集合</returns>
        public static IEnumerable<string> GetFilesAdv(string dirPath, bool isSearchSubDirs = true)
        {
            if (Directory.Exists(dirPath) == false)
            {
                throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + dirPath);
            }

            List<string> r = Directory.GetFiles(dirPath).ToList();

            if (isSearchSubDirs)
            {
                foreach (string subDirPath in Directory.GetDirectories(dirPath))
                {
                    var subR = GetFilesAdv(subDirPath, isSearchSubDirs);
                    r.AddRange(subR);
                }
            }

            return r;
        }

        /// <summary>
        /// 拷贝文件夹到指定文件夹
        /// </summary>
        /// <param name="sourceDirName">源文件夹路径</param>
        /// <param name="destDirName">粘贴到文件夹路径</param>
        /// <param name="isCopySubDirs">开启拷贝子文件夹文件, 默认 true</param>
        /// <param name="isOverwrite">覆盖已存在文件, 默认 true</param>
        public static void Copy(string sourceDirName, string destDirName, bool isCopySubDirs = true, bool isOverwrite = true)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            DirectoryInfo[] dirs = dir.GetDirectories();
            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, isOverwrite);
            }

            // If copying subdirectories, copy them and their contents to new location.
            if (isCopySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    Copy(subdir.FullName, temppath, isCopySubDirs, isOverwrite);
                }
            }
        }

        /// <summary>
        /// 绝对路径转换成相对路径
        /// </summary>
        /// <param name="fileInfoFullPath">待转换路径</param>
        /// <param name="baseDir">基础路径(根目录)</param>
        /// <returns>相对路径</returns>
        public static string GetRelativePath(string fileInfoFullPath, string baseDir)
        {
            if (fileInfoFullPath.IndexOf(baseDir) < 0)
            {
                throw new Exception($"待转换路径不包含基础路径。基础路径: {baseDir}; 待转换路径: {fileInfoFullPath}");
            }

            string temp = fileInfoFullPath.Replace(baseDir, "");
            if (temp.StartsWith("/") || temp.StartsWith("\\"))
            {
                return temp.Substring(1);
            }
            else 
            {
                return temp;
            }
        }
    }
}
